package leetcode;


import java.util.Stack;

/**
 * 有效的括号
 */
public class LeetCode20 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '{' || c == '(' || c == '[') {
                stack.push(c);

            } else {
                char t = stack.pop();
                if (c == '}' && t != '{') {
                    return false;
                }
                if (c == ')' && t != '(') {
                    return false;
                }
                if (c == ']' && t != '[') {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        LeetCode20 leetCode20 = new LeetCode20();
        System.out.println(leetCode20.isValid("()"));
    }
}